﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Linq.Expressions;

namespace BusinessLogic
{
    public class ShopLogic
    {

        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
                   where T : class
        {
            Model1Container db = new Model1Container();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

        public Shop GetForm(int keyValue)
        {
            Model1Container db = new Model1Container();
            return db.Shop.Find(keyValue);
        }

        public int AddInfo(Shop model)
        {
            int result = 0;
            try
            {
                Model1Container db = new Model1Container();
                db.Shop.Add(model);
                db.SaveChanges();
                result = model.ID;
            }
            catch (Exception ex)
            {
                return 0;
            }
            return result;
        }

        public int UpdateInfo(Shop model)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var newModel = db.Shop.Where(c => c.ID == model.ID).First();
                    newModel.ShopName = model.ShopName;
                    newModel.Pic = model.Pic;
                    newModel.OpenStartTime = model.OpenStartTime;
                    newModel.OpenEndTime = model.OpenEndTime;
                    newModel.SetUpTime = model.SetUpTime;
                    newModel.Lng = model.Lng;
                    newModel.Lat = model.Lat;
                    newModel.IsShow = model.IsShow;
                    newModel.ModifyDate = model.ModifyDate;
                    newModel.ModifyPerson = model.ModifyPerson;
                    newModel.Address = model.Address;
                    newModel.AreaQu = model.AreaQu;
                    newModel.Province = model.Province;
                    newModel.City = model.City;
                    newModel.IsOpen = model.IsOpen;
                    newModel.Details = model.Details;
                    newModel.Tel = model.Tel;
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int DelInfo(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Shop.First(p => p.ID == id);
                    model.Isdelete = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int Show(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Shop.First(p => p.ID == id);
                    model.IsShow = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int UnShow(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Shop.First(p => p.ID == id);
                    model.IsShow = 0;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int Open(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Shop.First(p => p.ID == id);
                    model.IsOpen = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int UnOpen(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Shop.First(p => p.ID == id);
                    model.IsOpen = 0;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int Pic_Shop(string ids, int ShopID)
        {
            int id = 0;
            if (!string.IsNullOrEmpty(ids))
            {
                using (Model1Container db = new Model1Container())
                {
                    db.T_Pic.RemoveRange(db.T_Pic.Where(c => c.SouceID == ShopID && c.Type == 1));
                    db.SaveChanges();
                }
                using (Model1Container db = new Model1Container())
                {

                    var temarry = ids.Split(',');
                    for (int i = 0; i < temarry.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(temarry[i]))
                        {
                            T_Pic model = new T_Pic();
                            model.SouceID = ShopID;
                            model.Url = temarry[i];
                            model.Type = 1;
                            model.Details = "";

                            db.T_Pic.Add(model);
                        }
                    }
                    db.SaveChanges();
                }
            }
            return id;
        }
    }
}
